import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;

public class Ricerca extends JFrame{

	private static final long serialVersionUID = 1L;
	
	private JFrame mw;
	private JTextField textCodice, textNome;
	private JButton btnRicerca, btnPulisci, btnChiudi;
	private JTable resultTable; 
	private JScrollPane scrollPane;
	private JPanel panel1;
	private GridBagConstraints gc;
	
	public Ricerca() {
		this.setTitle("Ricerca Prodotto");
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.addWindowListener(new Closer());
		
		panel1 = new JPanel();
		panel1.setLayout(new GridBagLayout());
		
		/*
		 * Default Constraints
		 */
		gc = new GridBagConstraints();
		gc.gridwidth = 1; 
		gc.gridheight = 1;
		gc.insets = new Insets(5,5,5,5); //External Padding
		gc.anchor = GridBagConstraints.EAST;
		gc.fill = GridBagConstraints.NONE;
		
		/*
		 * Labels
		 */
		gc.gridx = 0;
		gc.gridy = 0;
		panel1.add(new JLabel("Codice Prodotto:"),gc);
		
		gc.gridy = 1;
		panel1.add(new JLabel("Nome Prodotto:"), gc);
		
		/*
		 * Text Fields
		 */
		gc.gridx = 1;
		gc.gridwidth = 2;
		gc.anchor = GridBagConstraints.WEST;
		
		Selector ca = new Selector ();
		
		gc.gridy = 0;
		textCodice = new JTextField(30);
		textCodice.addActionListener(ca);
		panel1.add(textCodice, gc);
		
		gc.gridy = 1;
		textNome = new JTextField(30);
		textNome.addActionListener(ca);
		panel1.add(textNome, gc);	

		/*
		 * Buttons
		 */
		
		gc.gridx = 0;
		gc.anchor=GridBagConstraints.CENTER;
		gc.gridy = 3;
		gc.gridwidth = 3;
		Box buttonBox = Box.createHorizontalBox();
		btnRicerca = new JButton ("Ricerca");
		btnRicerca.addActionListener(ca);
		btnPulisci = new JButton("Pulisci");
		btnPulisci.addActionListener(ca);
		btnChiudi = new JButton("Chiudi");
		btnChiudi.addActionListener(ca);
		buttonBox.add(btnRicerca);
		buttonBox.add(Box.createHorizontalStrut(20));
		buttonBox.add(btnPulisci);
		buttonBox.add(Box.createHorizontalStrut(20));
		buttonBox.add(btnChiudi);
		
		panel1.add(buttonBox, gc);
		  
		/*
		 * Result Table
		 */
		gc.gridx=0;
		gc.gridy=4;
		gc.gridwidth=3;
		resultTable = new JTable ();
		resultTable.setAutoCreateRowSorter(true);
		resultTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		
        //Create the scroll pane and add the table to it.
        scrollPane = new JScrollPane(resultTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED , JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        //Add the scroll pane to this panel.
        panel1.add(scrollPane,gc);
		
		this.add(panel1);
		
		this.pack();
		
		this.setLocationRelativeTo(null);		
		this.setVisible(true);
		this.setResizable(false);
		
		textNome.requestFocus();
	}
	
	public Ricerca (JFrame mw) {
		this();
		this.mw = mw;
		mw.setVisible(false);	
	}
	
	private class MyTableModel extends AbstractTableModel {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		
		private String [] columnNames = {
				"Codice",
				"Nome",
				"Taglia", 
				"Quantita'",
				"Prezzo"
		};
		private Object[][] data;
		
		public MyTableModel(Object[][] risultato) { 
			data = risultato;			
		}
		
		@Override
		public int getRowCount() {
			return data.length;
		}

		@Override
		public int getColumnCount() {
			return columnNames.length;
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			return data[rowIndex][columnIndex];
		}
		
		public String getColumnName(int col) {
			return columnNames[col];
		}
		
		public void setValueAt(Object value, int row, int col) { 
			data[row][col] = value;
		}
		
		@SuppressWarnings({ "rawtypes", "unchecked" })
		public Class getColumnClass(int c) {
	        return getValueAt(0, c).getClass();
	    }
		
	}
	
	private class Selector implements ActionListener {

		private void eseguiRicerca() { 
			Object[][] risultato = null;
			if (textNome.getText().length()>0) {
				risultato = MagazzinoDB.ricercaPerNome(textNome.getText());	
			}
			if (textCodice.getText().length()>0) { 
				risultato = MagazzinoDB.ricercaPerCodice(textCodice.getText());
			}
			if (risultato != null) { 
				resultTable.setVisible(true);
				MyTableModel mm = new MyTableModel(risultato);
				resultTable.setModel(mm);
				resultTable.getColumnModel().getColumn(0).setMinWidth(75); //Codice
				resultTable.getColumnModel().getColumn(1).setMinWidth(200); //Nome
				resultTable.getColumnModel().getColumn(2).setMinWidth(50); //Taglia
				resultTable.getColumnModel().getColumn(3).setMinWidth(30); //Quantita
				resultTable.getColumnModel().getColumn(4).setMinWidth(30); //Prezzo
			} else { 
				resultTable.setVisible(false);
			}
		}
		
		private void cleanMask(ActionEvent e) { 
			textCodice.setText("");
			textNome.setText("");
			
			textNome.requestFocus();
				
		}
		
		private void cleanMask() {
			
			resultTable.setVisible(false);
			
			cleanMask(null);

		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource()==textCodice) { 
				eseguiRicerca();
				cleanMask(e);
			}
			if (e.getSource()==textNome) { 
				eseguiRicerca();
				cleanMask(e);
			}
			if (e.getSource()==btnRicerca) { 
				eseguiRicerca ();
				cleanMask(e);
				textNome.requestFocus();
			}
			if (e.getSource()==btnPulisci) { 
				cleanMask();
			}
			if (e.getSource()==btnChiudi) { 
				mw.setVisible(true);
				Ricerca.this.dispose();
			}
			
		}
		
	}
	
	private class Closer implements WindowListener {

		@Override
		public void windowOpened(WindowEvent e) {
			
		}

		@Override
		public void windowClosing(WindowEvent e) {
			mw.setVisible(true);
			Ricerca.this.dispose();
		}

		@Override
		public void windowClosed(WindowEvent e) {
		}

		@Override
		public void windowIconified(WindowEvent e) {
			Ricerca.this.pack();
		}

		@Override
		public void windowDeiconified(WindowEvent e) {
			Ricerca.this.pack();
		}

		@Override
		public void windowActivated(WindowEvent e) {
		}

		@Override
		public void windowDeactivated(WindowEvent e) {
		}
		
	}
	
}
